using System;
using Microsoft.SPOT;
using System.Net.Sockets;

namespace eHouse
{
    class TemperatureBL
    {
        private static TemperatureBL instance;
        
        public static TemperatureBL GetInstance()
        {
            if (instance == null)
                instance = new TemperatureBL();

            return instance;
        }

        public double GetTemperature()
        {
            return Drivers.getInstance().getTemperature();
        }

        public bool[] GetHeatingAC()
        {
            bool[] state = new bool[2];
            state[0] = Drivers.getInstance().led_heating.Read();
            state[1] = Drivers.getInstance().led_AC.Read();

            return state;
        }

        public void SetHeatingAC(string sensor, bool state)
        {
            switch (sensor)
            {
                case "Heating": Drivers.getInstance().led_heating.Write(state); break;
                case "AC": Drivers.getInstance().led_AC.Write(state); break;
            }
        }

        public Rule GetRule()
        {          

            string temp_rule = ConnectionManager.GetInstance().GetTempRules();
            temp_rule = temp_rule.Substring("RULES_TEMPERATURE/".Length);

            if (temp_rule != "")
            {
                string[] rule_parts = temp_rule.Split('\\');
                string id = rule_parts[0];
                int desiredTemp = int.Parse(rule_parts[1]);

                return new Rule(id, desiredTemp, null);
            }
            else
            {
                return null;
            }
            
        }

        public void AddRule(Rule rule)
        {
            int temperature = (int)rule.Body;
            ConnectionManager.GetInstance().AddTempRules(rule.ID + '\\' + temperature.ToString());
        }

        public void DeleteRule()
        {
            ConnectionManager.GetInstance().DeleteTempRule();
        }
    }
}
